/*
 * Copyright © OpenAtom Foundation.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.msu.server.storage.database.domain.manager;

import io.iec.edp.caf.msu.server.storage.database.domain.entity.GspMsuRegisterInfo;
import io.iec.edp.caf.msu.server.storage.database.domain.repository.GspMsuRegistryRepository;
import lombok.var;

import java.util.ArrayList;
import java.util.List;

public class GspMsuRegisterManager {

    private GspMsuRegistryRepository repository;

    public GspMsuRegisterManager(GspMsuRegistryRepository repository) {
        this.repository = repository;
    }

    public Boolean saveMsuInfo(List<GspMsuRegisterInfo> info){
        this.repository.saveAll(info);
        return true;
    }

    public List<GspMsuRegisterInfo> getMsuRegisterInfo(String appName, String serviceUnitName){
        return this.repository.findAllByAppNameAndServiceUnitName(appName,serviceUnitName);
    }

    public List<GspMsuRegisterInfo> getAllInfo(){
        return this.repository.findAll();
    }


    public Boolean deleteMsuInfo(String id){
        this.repository.deleteById(id);
        return true;
    }

    public void deleteService(List<String> serviceList){
        this.repository.deleteAllByServiceName(serviceList);
    }

    public void deleteBySu(List<String> sus){
        this.repository.deleteAllBySu(sus);
    }

    public void deleteByAppAndSu(String appName,List<String> sus){
        this.repository.deleteByAppNameAndServiceUnitNameIn(appName,sus);
    }

    public List<GspMsuRegisterInfo> getEnabledServiceUnitInfo(){
        var infos = this.repository.findAll();
        var sulist = new ArrayList<GspMsuRegisterInfo>();
        if(infos!=null){
            infos.forEach(x->{
               if(sulist.contains(x.getServiceUnitName())==false){
                   sulist.add(x);
               }
            });
        }

        return sulist;
    }
}
